﻿using System;
using Evpo.ContactData.ViewModel;
using System.Collections.Specialized;
using System.Web;
using Evpo.ContactData.Common;
using Evpo.ContactData.UI.GeneralClasses;

namespace Evpo.ContactData.UI
{
    partial class AddressList
    {
        protected class UrlComposer : ListUrlComposer
        {
            public UrlComposer(AddressListViewModel model, Uri baseUrl)
                : base(baseUrl)
            {
                _model = model;
            }

            #region Fields
            private readonly AddressListViewModel _model;

            #endregion

            #region Properties

            protected override ListViewModel ListViewModel
            {
                get { return _model; }
            }

            #endregion

            #region Methods

            public Uri AddressItemUrl(int addressID)
            {
                throw new NotImplementedException();
            }

            protected override void UpdateParametersInNameValueCollection(NameValueCollection collection, ListQueryParameters listParameters)
            {
                base.UpdateParametersInNameValueCollection(collection, listParameters);

                AddressQueryParameters parameters = (AddressQueryParameters)listParameters;
                
                UpdateParameterInCollection(collection, QueryKeys.LinesContain, parameters.LinesContain);
                UpdateParameterInCollection(collection, QueryKeys.SuburbContains, parameters.SuburbContains);
                UpdateParameterInCollection(collection, QueryKeys.State, parameters.State);
                UpdateParameterInCollection(collection, QueryKeys.Postcode, parameters.Postcode);
                UpdateParameterInCollection(collection, QueryKeys.Country, parameters.Country);
            }
            
            internal static AddressQueryParameters ExtractQueryParameters(NameValueCollection query)
            {
                var retVal = new AddressQueryParameters()
                {
                    PageBegin = 1,
                    PageEnd = Preferences.PageCount,
                };

                string paramValue = query[QueryKeys.PageBegin];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.PageBegin = Int32.Parse(paramValue);

                paramValue = query[QueryKeys.PageEnd];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.PageEnd = Int32.Parse(paramValue);

                paramValue = query[QueryKeys.LinesContain];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.LinesContain = paramValue;

                paramValue = query[QueryKeys.SuburbContains];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.SuburbContains = paramValue;

                paramValue = query[QueryKeys.State];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.State = paramValue;

                paramValue = query[QueryKeys.Postcode];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.Postcode = paramValue;

                paramValue = query[QueryKeys.Country];
                if (!String.IsNullOrEmpty(paramValue))
                    retVal.Country = paramValue;

                return retVal;
            }

            #endregion Methods
        }
    }
}
